<?php

namespace modules\Controller;

use modules\Model\UserModel;
use Think\Controller;

class IndexController extends Controller {
  public function Index() {
    $this->getUserUseModel();

  }

  public function getScope(){
    $userModel = new UserModel();
    $result = $userModel->getScope('001');
    $this->ajaxReturn($result,'JSON');
  }

  public function getAllScope(){
    $userModel = new UserModel();
    $result = $userModel->getScope('');
    $this->ajaxReturn($result,'JSON');
  }

  public function getUserUseModel(){
    $userModel = new UserModel();
    $result = $userModel->getUser("litong", "123");
    $sql = $userModel->getLastSql();
    array_push($result,$sql);
    $this->ajaxReturn($result);
  }

  public function findUserByNameFetchSql($name){
    $userModel = new UserModel();
    $params=array(
      "user"=>$name
    );
    $sql=$userModel->where($params)->fetchSql(true)->select();
    $this->ajaxReturn($sql);
  }
  public function findUserByNameAndPage($username,$pageNo,$pageSize){
    $userModel = new UserModel();
    $params=array(
      "user"=>$username,
    );
    $data = $userModel->where($params)->page(2, 10)->select();
    $sql = $userModel->getLastSql();
    array_push($data,$sql);
    $this->ajaxReturn($data,'JSON');
  }
  public function findUserByNameAndLimit($username,$start,$length){
    $userModel = new UserModel();
    $params=array(
      "user"=>$username
    );
    $data = $userModel->where($params)->limit($start, $length)->select();
    $sql = $userModel->getLastSql();
    array_push($data,$sql);
    $this->ajaxReturn($data, 'JSON');

  }
  public function findUserByIdAndName($id,$name){
    $userModel = new UserModel();
    $params=array(
      'id'=>$id,
      'user'=>$name
    );
    $data = $userModel->where($params)->select();
    return $data;
  }
  public function findUserByIdAndNameWithEQ($id,$name){
    $userModel = new UserModel();
    $params=array(
      'id'=>$id,
      'user'=>array("EQ",$name)
    );
    $data = $userModel->where($params)->select();
    return $data;
  }

  public function selete(){
    $userModel = new UserModel();
    $data = $userModel->select();
    return $data;
  }
  public function getFields(){
    $userModel = new UserModel();
    $fields = $userModel->getDbFields();
    return $fields;
  }

  public function addUser() {
    $params=array(
      "user"=>"zhaoda",
      "pswd"=>md5('12345')
    );
    $userModel = new UserModel();
    $userModel->add($params);
  }

  private function updateUserPasswordByName($id,$pswd){
    $userModel = new UserModel();
    $params=array(
      'id'=>$id,
      'pswd'=>$pswd
    );
    $userModel->save($params);
  }
  public function  deleteUserByid($id){
    $userModel = new UserModel();
    $userModel->delete($id);
  }

  public function  findUserById($id){
    $userModel = new UserModel();
    $data = $userModel->find($id);
    return $data;
  }

  public function changeDB() {
    $userModel = new UserModel();
    $data = $userModel->db(2, "DB_2")->table("user")->select();
    $this->ajaxReturn($data, 'JSON');
  }
}